package org.acooly.secretbox.service.impl;

import org.acooly.android.utils.DateUtils;
import org.acooly.secretbox.service.UserService;
import org.acooly.secretbox.service.domain.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class UserServiceImpl extends SQLiteOpenHelper implements UserService {

	private static final String DATABASE_NAME = "user.db";
	private static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS USER(ID INTEGER PRIMARY KEY AUTOINCREMENT, CREATETIME date not null,PASSWORD text not null,QUESTION text not null,ANSWER text not null,DEVICEINFO text );";

	public UserServiceImpl(Context context) {
		super(context, DATABASE_NAME, null, 1);
	}

	@Override
	public void update(User user) {
		SQLiteDatabase db = null;
		try {
			ContentValues values = new ContentValues();
			values.put("CREATETIME", DateUtils.formart(user.getCreateTime()));
			values.put("PASSWORD", user.getPassword());
			values.put("QUESTION", user.getQuestion());
			values.put("ANSWER", user.getAnswer());
			values.put("DEVICEINFO", user.getDeviceInfo());
			db = this.getWritableDatabase();
			db.update("User", values, "id=?", new String[] { user.getId() });
		} catch (Exception e) {
			Log.e("UserService", e.getMessage());
			throw new RuntimeException("用户注册失败:" + e.getMessage());
		} finally {
			if (db.isOpen()) {
				db.close();
			}
		}

	}

	@Override
	public void register(User user) {
		SQLiteDatabase db = null;
		try {
			ContentValues values = new ContentValues();
			values.put("CREATETIME", DateUtils.formart(user.getCreateTime()));
			values.put("PASSWORD", user.getPassword());
			values.put("QUESTION", user.getQuestion());
			values.put("ANSWER", user.getAnswer());
			values.put("DEVICEINFO", user.getDeviceInfo());
			db = this.getWritableDatabase();
			db.insert("USER", null, values);
		} catch (Exception e) {
			Log.e("UserService", e.getMessage());
			throw new RuntimeException("用户注册失败:" + e.getMessage());
		} finally {
			if (db.isOpen()) {
				db.close();
			}
		}
	}

	@Override
	public boolean haveAlreadyRegistered() {
		SQLiteDatabase db = null;
		try {
			db = this.getReadableDatabase();
			Cursor mCount = db.rawQuery("select count(*) from USER", null);
			mCount.moveToFirst();
			int count = mCount.getInt(0);
			mCount.close();
			return count > 0;
		} catch (Exception e) {
			Log.e("haveAlreadyRegistered", e.getMessage());
			return false;
		} finally {
			if (db.isOpen()) {
				db.close();
			}
		}
	}

	@Override
	public User get() {
		SQLiteDatabase db = getReadableDatabase();
		try {
			Cursor cur = db.rawQuery("select * from USER", null);
			User user = null;
			if (cur.moveToNext()) {
				user = new User();
				user.setId(cur.getString(0));
				user.setCreateTime(DateUtils.parse(cur.getString(1)));
				user.setPassword(cur.getString(2));
				user.setQuestion(cur.getString(3));
				user.setAnswer(cur.getString(4));
				user.setDeviceInfo(cur.getString(5));
				Log.i("Get User", user.toString());
			}
			cur.close();
			return user;
		} catch (Exception e) {
			Log.e("get", e.getMessage());
			throw new RuntimeException("用户信息不存在:" + e.getMessage());
		} finally {
			db.close();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i(DATABASE_NAME, TABLE_CREATE);
		db.execSQL(TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

}
